home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number3 / shades / shades.pas < prev    next >
Pascal/Delphi Source File  |  1991-06-13  |  6KB  |  232 lines

  1. { shades.pas -- Sample After Dark TPW DLL by Tom Swan }
  2.  
  3. {$X+}                   { Enable extended syntax }
  4.  
  5. library Shades;
  6.  
  7. {$R shades.res}         { Link in resources from this file }
  8.  
  9. uses WinTypes, WinProcs, ADUnit;
  10.  
  11. const
  12.   max_Index    = 100;   { Maximum number of shapes visible }
  13.   dx1: Integer = 4;     { Delta values for controlling }
  14.   dy1: Integer = 10;    {  the animation's personality. }
  15.   dx2: Integer = 3;
  16.   dy2: Integer = 9;
  17.  
  18. type
  19.   ShapeRec = record     { Describes each graphic shape }
  20.     X1, Y1, X2, Y2 : Integer;     { Location }
  21.     Color: TColorRef;             { RGB color }
  22.   end;
  23.  
  24. var
  25.   ShapeArray: array[0 .. max_Index - 1] of ShapeRec;
  26.   Index: Integer;       { Index for ShapeArray }
  27.   Erasing: Boolean;     { True if erasing old Shapes }
  28.  
  29. {----- Shades Graphics Routines -----}
  30.  
  31. {- Return -1 if N < 0 or +1 if N >= 0 }
  32. function Sign(N: Integer): Integer;
  33. begin
  34.   if N < 0 then Sign := -1 else Sign := 1
  35. end;
  36.  
  37. {- Create new shape, direction, and color }
  38. procedure MakeNewShape(Dc: HDC; R: TRect; Index: Integer);
  39.   procedure NewCoord(var C, Change: Integer; Max: Integer;
  40.     var Color: TColorRef);
  41.   var
  42.     Temp: Integer;
  43.   begin
  44.     Temp := C + Change;
  45.     if (Temp < 0) or (Temp > Max) then
  46.     begin
  47.       Change := Sign(-Change) * (3 + Random(12));
  48.       repeat
  49.         Color := GetNearestColor(Dc,
  50.           RGB(Random(256), Random(256), Random(256)))
  51.       until Color <> GetBkColor(Dc)
  52.     end else
  53.       C := Temp
  54.   end;
  55. begin
  56.   with ShapeArray[Index] do
  57.   begin
  58.     NewCoord(X1, dx1, R.Right, Color);
  59.     NewCoord(Y1, dy1, R.Bottom, Color);
  60.     NewCoord(X2, dx2, R.Right, Color);
  61.     NewCoord(Y2, dy2, R.Bottom, Color)
  62.   end
  63. end;
  64.  
  65. {- Draw or erase a shape identified by Index }
  66. procedure DrawShape(Dc: HDC; Index: Integer);
  67. var
  68.   OldPen, Pen: HPen;
  69.   OldROP: Integer;
  70. begin
  71.   with ShapeArray[Index] do
  72.   if X1 >= 0 then
  73.   begin
  74.     Pen := CreatePen(ps_Solid, 1, Color);
  75.     OldPen := SelectObject(Dc, Pen);
  76.     OldROP := SetROP2(Dc, r2_XorPen);
  77.     Rectangle(Dc, X1, Y1, X2, Y2);
  78.     SelectObject(Dc, OldPen);
  79.     DeleteObject(Pen);
  80.     SetROP2(Dc, OldROP)
  81.   end
  82. end;
  83.  
  84. {- Initialize graphics variables }
  85. procedure InitShades;
  86. var
  87.   I: Integer;
  88. begin
  89.   Index := 0;
  90.   Erasing := False;
  91.   for I := 0 to max_Index - 1 do
  92.     ShapeArray[I].X1 := -1
  93. end;
  94.  
  95. {----- After Dark Functions -----}
  96.  
  97. {- Early initializations. Not used }
  98. function DoPreInitialize: Integer;
  99. begin
  100.   DoPreInitialize := 1
  101. end;
  102.  
  103. {- Initialize new graphics }
  104. function DoInitialize: Integer;
  105. begin
  106.   InitShades;
  107.   DoInitialize := noError
  108. end;
  109.  
  110. {- Blank the display. Optional }
  111. function DoBlank: Integer;
  112. var
  113.   R: TRect;
  114. begin
  115.   with LpModule^.ptRgnSize do
  116.     SetRect(R, 0, 0, X, Y);
  117.   FillRect(DC, R, GetStockObject(black_Brush))
  118. end;
  119.  
  120. {- Draw one "frame" of the animation }
  121. function DoDrawFrame: Integer;
  122. var
  123.   R: TRect;
  124.   OldIndex: Integer;
  125. begin
  126.   with LPSystem^.ptScreenSize do
  127.     SetRect(R, 0, 0, X, Y);
  128.   OldIndex := Index;
  129.   if Index = max_Index - 1 then
  130.   begin
  131.     Index := 0;
  132.     Erasing := True
  133.   end else
  134.     Inc(Index);
  135.   if Erasing then DrawShape(Dc, Index);
  136.   ShapeArray[Index] := ShapeArray[OldIndex];
  137.   MakeNewShape(Dc, R, Index);
  138.   DrawShape(Dc, Index);
  139.   DoDrawFrame := noError
  140. end;
  141.  
  142. {- Shutdown animation }
  143. function DoClose: Integer;
  144. begin
  145.   InitShades;  { Reinitialize }
  146.   DoClose := noError
  147. end;
  148.  
  149. {- Initialize control panel. Not used }
  150. function DoSelected: Integer;
  151. begin
  152.   DoSelected := noError
  153. end;
  154.  
  155. {- Perform custom about-box graphics. Not used }
  156. function DoAbout: Integer;
  157. begin
  158.   DoAbout := noError
  159. end;
  160.  
  161. {- Respond to control panel buttons. Not used }
  162. function DoButtonMessage(IButtonID: Integer): Integer;
  163. begin
  164.   DoButtonMessage := noError
  165. end;
  166.  
  167. {- Message dispatcher. DO NOT MODIFY! }
  168. function Module(IMessage: Integer; HDrawDC: HDC;
  169.   HADSystem: THandle): Integer; export;
  170. var
  171.   IError: Integer;
  172.   I: Integer;
  173. begin
  174.   DC := HDrawDC;         { Save display context in global var }
  175.   HSystem := HADSystem;  { Save AD system handle in global var }
  176.   IError := 0;           { Unless changed by a function result }
  177.   LpSystem := GlobalLock(HSystem);
  178.   if LpSystem <> nil then
  179.   begin
  180.     LpModule := GlobalLock(LpSystem^.hModuleInfo);
  181.     if LpModule <> nil then
  182.     begin
  183.       case IMessage of
  184.         preInitialize:
  185.           IError := DoPreInitialize;
  186.         initialize:
  187.           begin
  188.             Randomize;
  189.             IError := Initialize
  190.           end;
  191.         blank:
  192.           IError := DoBlank;
  193.         drawFrame:
  194.           IError := DoDrawFrame;
  195.         adClose:
  196.           IError := DoClose;
  197.         moduleSelected:
  198.           begin
  199.             LpModule^.hModule := hLibInst;
  200.             for I := 0 to 3 do
  201.               LpModule^.iControlID[I] := I + 1;
  202.             IError := DoSelected
  203.           end;
  204.         about:
  205.           IError := DoAbout;
  206.         buttonMessage .. buttonMessage + 3:
  207.           IError := DoButtonMessage(IMessage - buttonMessage);
  208.       end;
  209.       GlobalUnlock(LpSystem^.HModuleInfo)
  210.     end;
  211.     GlobalUnlock(HSystem)
  212.   end;
  213.   Module := IError
  214. end;
  215.  
  216. {- Export DLL public routines }
  217.  
  218. exports
  219.   Module index 1;
  220.  
  221. {- DLL entry code }
  222.  
  223. begin
  224.   HLibInst := HInstance
  225. end.
  226.  
  227.  
  228. {--------------------------------------------------------------
  229.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  230.   Revision 1.00    Date: 6/12/1991
  231. ---------------------------------------------------------------}
  232.